package com.springboot.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.common.CustomException;
import com.springboot.dao.CategoryMapper;
import com.springboot.pojo.Category;
import com.springboot.pojo.Dish;
import com.springboot.pojo.Setmeal;
import com.springboot.service.CategoryService;
import com.springboot.service.DishService;
import com.springboot.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    public void remaveCategory(Long id) {
        //删除分类及其下属菜品
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
          wrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(wrapper);
        if(count1>0){
          throw new CustomException("该分类下有"+count1+"个菜品，不能删除");
        }

        //删除分类及其下属套餐
        LambdaQueryWrapper<Setmeal> wrapper2=new LambdaQueryWrapper<>();
          wrapper2.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count(wrapper2);
        if(count2>0) {
            throw new CustomException("该分类下有"+count2+"个套餐不能删除");
        }
        super.removeById(id);
    }
}
